﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NDesk.Options;
using AmaroK86.MassEffect3;

namespace AmaroK86.MassEffect3
{
    class DLCSwitchFileExec
    {
        private static string GetExecutableName()
        {
            return Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
        }

        static void Main(string[] args)
        {
            bool showHelp = false;
            bool verbose = false;
            bool force = false;

            var options = new OptionSet()
            {
                {
                    "f|force",
                    "force replace of old file, use it at your own risk!",
                    v => force = v != null
                },
                {
                    "v|verbose",
                    "be verbose",
                    v => verbose = v != null
                },
                {
                    "h|help",
                    "show this message and exit", 
                    v => showHelp = v != null
                },
            };

            List<string> extras;

            try
            {
                extras = options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("{0}: ", GetExecutableName());
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName());
                return;
            }

            if (extras.Count < 1 || extras.Count > 2 || showHelp == true)
            {
                Console.WriteLine("Usage: {0} [OPTIONS]+ input_sfar file_to_replace", GetExecutableName());
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return;
            }

            string oldSfar = extras[0];
            string newSfar = Path.GetFileNameWithoutExtension(oldSfar) + "_new" + Path.GetExtension(oldSfar);
            string fileToReplace = extras[1];
            string filePathToReplace = fileToReplace;
            bool found = false;
            //DLCSwitchFile.SwitchFile(extras[0], extras[1], extras[1], newSfar, null);

            DLCBase dlcBase = new DLCBase(oldSfar);
            DLCEditor dlcEditor = new DLCEditor(dlcBase);
            foreach (sfarFile entry in dlcBase.fileList)
            {
                if (entry.nameHash != DLCBase.fileListHash)
                {
                    string[] fileNameCnks = entry.fileName.Split('/');
                    if (String.Compare(fileNameCnks[fileNameCnks.Length - 1], fileToReplace) == 0 ||
                       String.Compare(entry.fileName, filePathToReplace) == 0)
                    {
                        found = true;
                        dlcEditor.setReplaceFile(entry.fileName, fileToReplace);
                    }
                }
            }

            //dlcEditor.setAddFile("/BIOGame/DLC_HEN_PR_ITA.tlk", "DLC_HEN_PR_ITA.tlk");
            //dlcEditor.setDeleteFile("/BIOGame/DLC/DLC_HEN_PR/CookedPCConsole/Textures_DLC_HEN_PR.tfc");

            if (verbose)
                dlcEditor.verbose = true;

            if (found)
                dlcEditor.Execute(newSfar);

            if (force)
            {
                File.Delete(oldSfar);
                File.Move(newSfar, oldSfar);
            }
        }
    }
}
